package com.example.demo.log;

import com.example.demo.entity.SysLog;
import com.example.demo.service.LogService;
import com.example.demo.util.SpringContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author zhanpengguo
 * @date 2019-07-09 11:14
 */
@Slf4j
@Aspect
@Component
public class LogAspect {

    @Around("@annotation(myLog)")
    public Object around(ProceedingJoinPoint point,MyLog myLog) throws Throwable{
        String className = point.getTarget().getClass().getName();
        String methodName = point.getSignature().getName();
        String value = myLog.value();
        log.info("类名：{}，方法名：{}，注解值：{}",className,methodName,value);
        SysLog sysLog = new SysLog();
        sysLog.setName(value);
        long startTime = System.currentTimeMillis();
        Object proceed = point.proceed();
        long endTime = System.currentTimeMillis();
        long time = endTime - startTime;
        sysLog.setTime(time);
        SpringContextHolder.publishEvent(new MyLogEvent(sysLog));
        return proceed;
    }
}
